package com.campustradingwall.controller.user;

import com.campustradingwall.dao.OrderDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/updateOrderStatus")
public class UpdateOrderStatusServlet extends HttpServlet {
    private OrderDao orderDao = new OrderDao();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int orderId = Integer.parseInt(request.getParameter("id"));
            String status = request.getParameter("status");

            if (status != null && ("completed".equals(status) || "cancelled".equals(status))) {
                // 在实际应用中，还应验证操作的合法性
                // 例如，只有卖家能"完成"订单，只有买家能"取消"订单
                orderDao.updateOrderStatus(orderId, status);
                request.getSession().setAttribute("message", "订单状态更新成功！");
            } else {
                request.getSession().setAttribute("message", "无效的操作！");
            }
        } catch (NumberFormatException e) {
            request.getSession().setAttribute("message", "更新失败：无效的订单ID。");
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/user/orders");
    }
} 